
class UserSystem 
{
    static mInstance = null

    static share () 
    {
        this.mInstance || (this.mInstance = new UserSystem())

        return this.mInstance
    }

    constructor()
    {
        this.mHostUser = null
    }

    close()
    {
        this.destroyUser()
    }

    login(d, onSuccess, onFail) 
    {
        let sdk = ogre.sdkPlatform

        let requestLogin = (third) =>{

            if (this.mHostUser && third && (!third.id || third.id < 0))
            {
                const User_base = require('User_base')

                third.id = this.mHostUser.contentBy(User_base.thirdId)
            }

            require('NetProtocol').login(third, 
                (data) => {

                    if (data && data.rs == 0) 
                    {
                        this.createHostUser(data)

                        if (sdk) 
                        {
                            sdk.trackEvent("login")
                        }
                        onSuccess(data)
                    }
                    else 
                    {
                        onFail(data)
                    }
                }
            )
        }
        
        if (sdk)
        {
            sdk.login(d, requestLogin)
        }
        else
        {
            requestLogin()
        }
    }

    logout(callback) 
    {
        if (!this.mHostUser)
        {
            callback && callback(true)

            return
        }

        let logoutCallback = (ok) =>{

            if (ok)
            {
                require('NetProtocol').logout(
                    (m) =>{

                        if (m && m.rs == 0)
                        {
                            this.destroyUser()

                            callback && callback(true)
                        }
                        else
                        {
                            callback && callback(false)
                        }
                    }
                )
            }
            else
            {
                callback && callback(false)
            }
        }   

        if (ogre.sdkPlatform)
        {
            ogre.sdkPlatform.logout(null, logoutCallback)
        }
        else
        {
            logoutCallback(true)
        }
    }

    bind() 
    {

    }

    createHostUser(data) 
    {
        if (!this.mHostUser) 
        {

            let User = require('User')

            this.mHostUser = new User()
        }

        this.mHostUser.sysncContent(data)

        // let sdk = ogre.sdkPlatform

        // if (sdk && sdk.avatarBy())
        // {
        //     this.mHostUser.updateAvatarUrl(sdk.avatarBy(), true)
        // }
        // else
        // {
        //     this.mHostUser.updateAvatarUrl("", true)
        // }
    }

    destroyUser(user)
    {
        if (user)
        {
            user.close()
        }
        else
        {
            if (this.mHostUser)
            {
                this.mHostUser.close()

                this.mHostUser = null
            }
        }
    }

    hostUser() 
    {
        return this.mHostUser
    }
}

export default UserSystem
